#!/usr/bin/env python
# -*- coding: utf-8 -*-

import json
from score_calculator import calculate_score

def run_tests():
    """运行一系列测试，检查评分计算的正确性"""
    
    test_cases = [
        # 新华财经
        {"platform": "新华财经", "has_media": False, "expected": 3},
        {"platform": "新华财经", "has_media": True, "expected": 7},
        
        # 学习强国
        {"platform": "学习强国", "has_media": False, "expected": 3},
        {"platform": "学习强国", "has_media": True, "expected": 7},
        
        # 国家局网站
        {"platform": "国家局网站", "expected": 4},
        
        # 东方烟草报
        {"platform": "东方烟草报", "article_type": "纯文学类", "expected": 1.5},
        {"platform": "东方烟草报", "article_type": "通讯类", "is_front_page": True, "expected": 2.5},
        {"platform": "东方烟草报", "article_type": "通讯类", "is_front_page": False, "expected": 2},
        {"platform": "东方烟草报", "article_type": "通讯类", "is_front_page": False, "image_count": 3, "expected": 3},  # 2 + min(3*0.5, 1)
        {"platform": "东方烟草报", "article_type": "新闻", "expected": 0.5},
        {"platform": "东方烟草报", "article_type": "专题栏目类", "image_count": 5, "expected": 2},  # 受2分封顶限制
        
        # 浙江烟草杂志
        {"platform": "浙江烟草杂志", "article_type": "文字类", "fee": 50, "expected": 0.5},
        {"platform": "浙江烟草杂志", "article_type": "文字类", "fee": 150, "expected": 1},
        {"platform": "浙江烟草杂志", "article_type": "文字类", "fee": 300, "expected": 1.2},
        {"platform": "浙江烟草杂志", "article_type": "文字类", "fee": 400, "expected": 1.5},
        {"platform": "浙江烟草杂志", "article_type": "图片类", "fee": 50, "expected": 0.5},
        {"platform": "浙江烟草杂志", "article_type": "图片类", "fee": 100, "expected": 0.7},
        {"platform": "浙江烟草杂志", "article_type": "图片类", "fee": 150, "expected": 0.9},  # 0.7 + 0.2
        {"platform": "浙江烟草杂志", "article_type": "图片类", "fee": 250, "expected": 1},  # 达到1分封顶
        
        # 《中烟报》
        {"platform": "《中烟报》", "article_type": "新闻类", "fee": 20, "expected": 0.5},
        {"platform": "《中烟报》", "article_type": "新闻类", "fee": 30, "expected": 0.7},  # 0.5 + 0.2
        {"platform": "《中烟报》", "article_type": "新闻类", "fee": 60, "expected": 1},  # 达到1分封顶
        {"platform": "《中烟报》", "article_type": "通讯类", "fee": 30, "expected": 1},
        {"platform": "《中烟报》", "article_type": "通讯类", "fee": 40, "expected": 1.2},  # 1 + 0.2
        {"platform": "《中烟报》", "article_type": "通讯类", "fee": 80, "expected": 1.5},  # 达到1.5分封顶
        {"platform": "《中烟报》", "article_type": "图片类", "fee": 20, "expected": 0.3},
        {"platform": "《中烟报》", "article_type": "图片类", "fee": 40, "expected": 0.7},  # 0.3 + (2 * 0.2)
        {"platform": "《中烟报》", "article_type": "其他文学类", "fee": 10, "expected": 0.2},
        {"platform": "《中烟报》", "article_type": "其他文学类", "fee": 20, "expected": 0.5},
        {"platform": "《中烟报》", "article_type": "其他文学类", "fee": 30, "expected": 1},
        {"platform": "《中烟报》", "article_type": "其他文学类", "fee": 50, "expected": 1.4},  # 1 + (2 * 0.2)
        {"platform": "《中烟报》", "article_type": "达人", "expected": 1.2},
        {"platform": "《中烟报》", "article_type": "专版类", "expected": 4},  # 专版类固定加4分，不受2分封顶限制
        
        # 中烟网站
        {"platform": "中烟网站", "expected": 1},
        
        # 东烟视频网
        {"platform": "东烟视频网", "expected": 2},
        
        # 新华财经网
        {"platform": "新华财经网", "expected": 3},
    ]
    
    all_passed = True
    failed_count = 0
    
    print("开始测试评分计算...")
    print("=" * 60)
    
    for i, case in enumerate(test_cases, 1):
        expected = case.pop("expected")
        test_data = case.copy()
        
        actual = calculate_score(test_data)
        passed = abs(actual - expected) < 0.0001  # 浮点数比较
        
        result = "✓ 通过" if passed else "✗ 失败"
        status = "" if passed else f"预期: {expected}, 实际: {actual}"
        
        print(f"测试 {i}: {json.dumps(test_data, ensure_ascii=False)} - {result} {status}")
        
        if not passed:
            all_passed = False
            failed_count += 1
    
    print("=" * 60)
    if all_passed:
        print("所有测试全部通过！")
    else:
        print(f"测试完成，{failed_count} 个测试失败。")

if __name__ == "__main__":
    run_tests() 